﻿using DadayunApi.Model;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DadayunSync
{
    public partial class RestApiTester : Form
    {
        public RestApiTester()
        {
            InitializeComponent();
        }
        AuthBase authBase = new AuthBase
        {
            ClientId = "c514ea09ccdd4435a636443c0331d438",
            ClientSecret = "c7ea1d12c4dd456f9019c47ed638b4b3",
            Username = "admin",
            GrantType = "password",
            Password = "kbT8046",
            TenantName = "kenbotong",
            Scope = "openapi offline_access",
            PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFXMJ+EEgSGCXSy5ORYAUX1yn3tCGC6xCvJACcyK9cwqc5Wglp2cAY7dqrXUH8AJD+kwSTFMxPWeusl6i8JuJB0bktSmgqBhhq979DbSotZqOWAjRUNzGeph/fTXUDIzZ8AvNZOCUcHbIZP//60QWCc7fsEDPRBgStpIgXA03KL63AWyMbU92sXe/rLN1nxAkgzRLOcL87NdQTNBpHYRIoIp1OP7chXzLOq+J4ZfNHhR7bLujIg8M0C42ZIjl52YV7m4D69ZzChDW+1/zWXHl35I8kOjvH+1rjlkaq1kKO5vJ4ROeHAhSJOjQ4Jjz5zfXKGiXg9F6yp7zpfvUzpuDQIDAQAB"
        };
        string baseUri = "https://api.dadayun.cn";
        
        AccessToken token = null;
        private void button1_Click(object sender, EventArgs e)
        {
            string uri = "connect/token";
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            client.Authenticator = new HttpBasicAuthenticator(authBase.ClientId, authBase.ClientSecret);
            request.AddParameter("client_id", authBase.ClientId);
            request.AddParameter("appSecret", authBase.ClientSecret);
            request.AddParameter("grant_type", "password");
            request.AddParameter("username", authBase.Username);
            request.AddParameter("password", authBase.Password);
            //request.AddParameter("scope", "openapi offline_access");//可不用
            IRestResponse<AccessToken> response = client.Execute<AccessToken>(request);
            if (response.IsSuccessful)
            {
                staInfo.Text = response.StatusDescription;
                token = response.Data;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            string uri = "connect/token";
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            client.Authenticator = new HttpBasicAuthenticator(authBase.ClientId, authBase.ClientSecret);
            request.AddParameter("client_id", authBase.ClientId);
            request.AddParameter("appSecret", authBase.ClientSecret);
            request.AddParameter("grant_type", "refresh_token");
            request.AddParameter("refresh_token", token.Refresh_token);
            //request.AddParameter("scope", "openapi offline_access");//可不用
            IRestResponse<AccessToken> response = client.Execute<AccessToken>(request);
            if (response.IsSuccessful)
            {
                staInfo.Text = response.StatusDescription;
                token = response.Data;
            }
        }

        

        private void btnGetTime_Click(object sender, EventArgs e)
        {
            string uri = "v1/date";
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.GET);
            request.AddHeader("Authorization", "Bearer " + token.Access_token);
            IRestResponse<DateTime> response = client.Execute<DateTime>(request);
            if (response.IsSuccessful)
            {
                staInfo.Text = response.StatusDescription;
                DateTime t = response.Data;
            }
        }

        private void btnBatch_Click(object sender, EventArgs e)
        {
           
        }
    }

    public class AuthBase
    {
        /// <summary>
        /// 申请应用时分配的AppKey。
        /// </summary>
        public string ClientId { get; set; }
        /// <summary>
        /// 申请应用时分配的AppSecret。
        /// </summary>
        public string ClientSecret { get; set; }
        /// <summary>
        /// 授权类型，此处的值固定为password。 默认值: password
        /// </summary>
        public string GrantType { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        /// <summary>
        /// 申请scope权限所需参数，可一次申请多个scope权限，用空格分隔。
        /// </summary>
        public string Scope { get; set; }
        public string TenantName { get; set; }
        public string PublicKey { get; set; }
        public string AppKey { get { return ClientId; } }
        public string AppSecret { get { return ClientSecret; } }
    }
}
